package com.ordos.recruit.filter.config;

import cn.hutool.core.text.CharSequenceUtil;
import com.ordos.recruit.exception.SystemStartException;
import com.ordos.recruit.filter.SsoTokenFilter;
import com.ordos.recruit.module.user.constant.Conf;
import com.ordos.recruit.module.user.helper.JedisUtilHelper;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;

import javax.annotation.PostConstruct;

/**
 * Filter Config
 * @author jn
 */
@Configuration
public class SsoTokenFilterConfig implements DisposableBean {

    private String ssoLogoutPath;

    private String ssoExcludedPaths;

    private final Environment environment;

    public SsoTokenFilterConfig(Environment environment) {
        this.environment = environment;
    }

    @PostConstruct
    public void init() {
        String ssoLogoutPath = environment.getProperty("sso.logout.path");
        String ssoExcludedPaths = environment.getProperty("sso.excluded.paths");
        if (CharSequenceUtil.isBlank(ssoLogoutPath)) {
            throw new SystemStartException("当前应用中sso.logout.path属性为空, 请检查.");
        }
        if (CharSequenceUtil.isBlank(ssoExcludedPaths)) {
            ssoExcludedPaths = "";
        }
        this.ssoLogoutPath = ssoLogoutPath;
        this.ssoExcludedPaths = ssoExcludedPaths;
    }

    @Bean
    public FilterRegistrationBean<SsoTokenFilter> ssoFilterRegistration(RedisConnectionFactory redisConnectionFactory) {

        if (redisConnectionFactory instanceof LettuceConnectionFactory) {
            LettuceConnectionFactory lettuceConnectionFactory = ((LettuceConnectionFactory) redisConnectionFactory);
            String hostName = lettuceConnectionFactory.getHostName();
            int port = lettuceConnectionFactory.getPort();
            int database = lettuceConnectionFactory.getDatabase();
            String password = lettuceConnectionFactory.getPassword();
            //sso, redis init
            JedisUtilHelper.init(CharSequenceUtil.format("redis://:{}@{}:{}/{}",password, hostName, port, database));
        } else {
            throw new SystemStartException("系统错误，请添加相关业务逻辑。");
        }

        //sso, filter init
        FilterRegistrationBean<SsoTokenFilter> registration = new FilterRegistrationBean<>();
        registration.setName("ssoWebFilter");
        registration.setOrder(1);
        registration.addUrlPatterns("/*");
        registration.setFilter(new SsoTokenFilter());
        registration.addInitParameter(Conf.SSO_LOGOUT_PATH, ssoLogoutPath);
        registration.addInitParameter(Conf.SSO_EXCLUDED_PATHS, ssoExcludedPaths);
        return registration;
    }

    @Override
    public void destroy() throws Exception {
        //sso, redis close
        JedisUtilHelper.close();
    }

}
